

#include "../common/message.hpp"
#include "../common/net.hpp"
#include "../common/dispatcher.hpp"
#include <muduo/base/Logging.h>
#include "../server/rpc_router.hpp"
#include <vector>
bool Add(const Json::Value &req, Json::Value &res)
{
    int int_a = req["nums1"].asInt(), int_b = req["nums2"].asInt();
    int ret = int_a + int_b;
    res["ret"] = ret;
    return true;
}

int main()
{
    // 取消muduo库的普通日志
    muduo::Logger::setLogLevel(muduo::Logger::FATAL);

    // 1、注册一个方法
    RPC::Server::SDFactory::Ptr sfactory = std::make_shared<RPC::Server::SDFactory>();
    sfactory->setCallback(Add);
    sfactory->setMethod("Add");
    std::vector<RPC::Server::ServiceDiscribe::ParamsDescribe> params;
    params.push_back({"nums1", RPC::Server::VType::INTEGRAL});
    params.push_back({"nums2", RPC::Server::VType::INTEGRAL});
    sfactory->setParamsDescribe(params);
    sfactory->setReturnType(RPC::Server::VType::BOOL);
    RPC::Server::ServiceDiscribe::Ptr service1 = sfactory->build();
    // 2、创建route模块，注册这个方法
    RPC::Server::RpcRouter::Ptr route = RPC::Server::RouterFactory::create();
    route->registerRequest(service1);

    // 3、搞一个任务分发器，将route里面的回调方法注册进来
    RPC::Dispatcher dispatcher;
    RPC::CallBackT<RPC::RpcRequest>::MsgCallback call = std::bind(&RPC::Server::RpcRouter::onRpcRequest,
                                                                  route.get(), std::placeholders::_1, std::placeholders::_2);
    dispatcher.registerCallback<RPC::RpcRequest>(RPC::MType::REQ_RPC, call);

    // 4、构建服务器，设置回调方法，将任务分发器里面的回调注册进来
    auto server = RPC::ServerFactory::create(9090);
    auto onMessage = std::bind(&RPC::Dispatcher::onMessage, &dispatcher,
                               std::placeholders::_1, std::placeholders::_2);
    server->setMessageCallback(onMessage);

    server->start();
    return 0;
}